home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 58 / pcpp58a.iso / extras / quake 3 source / Q3A_ToolSource.exe / Main / mutex.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-02  |  2.9 KB  |  177 lines

  1.  
  2. #include "cmdlib.h"
  3. #include "threads.h"
  4. #include "mutex.h"
  5.  
  6. /*
  7. ===================================================================
  8.  
  9. WIN32
  10.  
  11. ===================================================================
  12. */
  13. #ifdef WIN32
  14.  
  15. #define    USED
  16.  
  17. #include <windows.h>
  18.  
  19. void MutexLock (mutex_t *m)
  20. {
  21.     CRITICAL_SECTION *crit;
  22.  
  23.     if (!m)
  24.         return;
  25.     crit = (CRITICAL_SECTION *) m;
  26.     EnterCriticalSection (crit);
  27. }
  28.  
  29. void MutexUnlock (mutex_t *m)
  30. {
  31.     CRITICAL_SECTION *crit;
  32.  
  33.     if (!m)
  34.         return;
  35.     crit = (CRITICAL_SECTION *) m;
  36.     LeaveCriticalSection (crit);
  37. }
  38.  
  39. mutex_t *MutexAlloc(void)
  40. {
  41.     CRITICAL_SECTION *crit;
  42.  
  43.     if (numthreads == 1)
  44.         return NULL;
  45.     crit = (CRITICAL_SECTION *) malloc(sizeof(CRITICAL_SECTION));
  46.     InitializeCriticalSection (crit);
  47.     return (void *) crit;
  48. }
  49.  
  50. #endif
  51.  
  52. /*
  53. ===================================================================
  54.  
  55. OSF1
  56.  
  57. ===================================================================
  58. */
  59.  
  60. #ifdef __osf__
  61. #define    USED
  62.  
  63. #include <pthread.h>
  64.  
  65. void MutexLock (mutex_t *m)
  66. {
  67.     pthread_mutex_t    *my_mutex;
  68.  
  69.     if (!m)
  70.         return;
  71.     my_mutex = (pthread_mutex_t *) m;
  72.     pthread_mutex_lock (my_mutex);
  73. }
  74.  
  75. void MutexUnlock (mutex_t *m)
  76. {
  77.     pthread_mutex_t    *my_mutex;
  78.  
  79.     if (!m)
  80.         return;
  81.     my_mutex = (pthread_mutex_t *) m;
  82.     pthread_mutex_unlock (my_mutex);
  83. }
  84.  
  85. mutex_t *MutexAlloc(void)
  86. {
  87.     pthread_mutex_t    *my_mutex;
  88.     pthread_mutexattr_t    mattrib;
  89.  
  90.     if (numthreads == 1)
  91.         return NULL;
  92.     my_mutex = malloc (sizeof(*my_mutex));
  93.     if (pthread_mutexattr_create (&mattrib) == -1)
  94.         Error ("pthread_mutex_attr_create failed");
  95.     if (pthread_mutexattr_setkind_np (&mattrib, MUTEX_FAST_NP) == -1)
  96.         Error ("pthread_mutexattr_setkind_np failed");
  97.     if (pthread_mutex_init (my_mutex, mattrib) == -1)
  98.         Error ("pthread_mutex_init failed");
  99.     return (void *) my_mutex;
  100. }
  101.  
  102. #endif
  103.  
  104. /*
  105. ===================================================================
  106.  
  107. IRIX
  108.  
  109. ===================================================================
  110. */
  111.  
  112. #ifdef _MIPS_ISA 
  113. #define    USED
  114.  
  115. #include <task.h>
  116. #include <abi_mutex.h>
  117. #include <sys/types.h>
  118. #include <sys/prctl.h>
  119.  
  120. void MutexLock (mutex_t *m)
  121. {
  122.     abilock_t *lck;
  123.  
  124.     if (!m)
  125.         return;
  126.     lck = (abilock_t *) m;
  127.     spin_lock (lck);
  128. }
  129.  
  130. void MutexUnlock (mutex_t *m)
  131. {
  132.     abilock_t *lck;
  133.  
  134.     if (!m)
  135.         return;
  136.     lck = (abilock_t *) m;
  137.     release_lock (lck);
  138. }
  139.  
  140. mutex_t *MutexAlloc(void)
  141. {
  142.     abilock_t *lck;
  143.  
  144.     if (numthreads == 1)
  145.         return NULL;
  146.     lck = (abilock_t *) malloc(sizeof(abilock_t));
  147.     init_lock (lck);
  148.     return (void *) lck;
  149. }
  150.  
  151. #endif
  152.  
  153. /*
  154. =======================================================================
  155.  
  156.   SINGLE THREAD
  157.  
  158. =======================================================================
  159. */
  160.  
  161. #ifndef USED
  162.  
  163. void MutexLock (mutex_t *m)
  164. {
  165. }
  166.  
  167. void MutexUnlock (mutex_t *m)
  168. {
  169. }
  170.  
  171. mutex_t *MutexAlloc(void)
  172. {
  173.     return NULL;
  174. }
  175.  
  176. #endif
  177.